//==================商家端======================

const dataKit = require( "./util/dataKit")
const dateKit = require( "./util/dateKit")
const businessService = require( "./util/businessService")

/**
 * 登录（保存token）
 */
exports.login = async (context) => {
    const {userNick} = context.data;
    const {accessToken,openId} = context;
    if(!userNick){
        return {code:-1,msg:"userNick是必须的"};
    }
    if(!accessToken){
        return {code:-1,msg:"请使用真机调试",data:"accessToken是必须的"};
    }
    
    let merchant = await businessService.getMerchantByOpenId(context,openId);
    if(!merchant){
        merchant = {
            openId:openId,
            createTime:dateKit.getCurrentDateTime()
        };
    }
    
    merchant.loginTime =  dateKit.getCurrentDateTime();
    merchant.userNick = userNick;
    merchant.accessToken = accessToken;
    const result = await dataKit.insertOrUpdate(context,"merchant_info",merchant);
    return {code:0,msg:"登录成功"};
};


/**
 * 获取商品列表
 */
exports.getProductList = async (context) => {
    let result = await businessService.getProductList(context,context.openId);
    return {code:0,msg:"获取成功",data:result};
};


